package com.microcity.autocomponent.autoview;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.microcity.autocomponent.R;

/**
 * PopWindow
 */
public class SelectPicturePopupWindow extends PopupWindow implements View.OnClickListener, PopupWindow.OnDismissListener {
    private View mContentView;
    private Context mContext;
    private PictureCallBack mPictureCallBack;

    public SelectPicturePopupWindow(Context context, PictureCallBack pictureCallBack) {
        super(context);
        this.mContext = context;
        this.mPictureCallBack = pictureCallBack;
        init(context);
    }

    private void init(Context context) {
        mContentView = LayoutInflater.from(context).inflate(R.layout.popup_select_picture, null);
        mContentView.setOnClickListener(this);        // 设置SelectPicPopupWindow的View
        this.setContentView(mContentView);        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);        // 刷新状态
        this.update();        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        TextView cancel = (TextView) mContentView.findViewById(R.id.popup_cancel);
        TextView gallery = (TextView) mContentView.findViewById(R.id.popup_select_from_gallery);
        TextView takePicture = (TextView) mContentView.findViewById(R.id.popup_take_picture);
        cancel.setOnClickListener(this);
        gallery.setOnClickListener(this);
        takePicture.setOnClickListener(this);
        setOnDismissListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.popup_cancel://取消
                break;
            case R.id.popup_select_from_gallery://图片图库
                if (mPictureCallBack != null) {
                    mPictureCallBack.onSelectFromGallery();
                }
                break;
            case R.id.popup_take_picture://拍照
                if (mPictureCallBack != null) {
                    mPictureCallBack.onTakePicture();
                }
                break;
        }
        dismiss();
    }

    @Override
    public void onDismiss() {
        dismiss();
    }

    public interface PictureCallBack {
        void onTakePicture();

        void onSelectFromGallery();
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void show(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            showAtLocation(parent, Gravity.NO_GRAVITY, 0, 0);
        } else {
            this.dismiss();
        }
    }
}